package cn.maydaymemory.mastery.command;

import cn.maydaymemory.mastery.PluginMastery;
import cn.maydaymemory.mastery.annotation.CommandHandler;
import cn.maydaymemory.mastery.command.api.CommandStructure;
import cn.maydaymemory.mastery.shop.SellInventory;
import cn.maydaymemory.mastery.shop.Shop;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CommandMasteryAdmin extends CommandStructure {
    public CommandMasteryAdmin(){
        setDescription("descriptions.MasteryAdmin",PluginMastery.getInstance().getCurrent_lang());
    }

    @CommandHandler(
            name = "reload",
            permission = "mastery.admin",
            parameters = {})
    public void reload(){
        PluginMastery.getInstance().reload();
        sender.sendMessage(" §7§l[Mastery] "+PluginMastery.getInstance().getCurrent_lang().getString("reloaded"));
    }

    @CommandHandler(
            name = "help",
            permission = "mastery.admin",
            parameters = {},
            visable = false)
    public void help(){
        sendHelp(1);
    }

    @CommandHandler(
            name = "help",
            permission = "mastery.admin",
            parameters = {"[页码]"})
    public void help2(){
        int page = Integer.valueOf(args[0]);
        sendHelp(page);
    }

    @CommandHandler(
            playerOnly = true,
            name = "set",
            permission = "mastery.admin",
            parameters = {"[价格]","[货币类型]"})
    public void set(){
        int price = Integer.valueOf(args[0]);
        String type = args[1];
        File fileGoodsConfig = new File(PluginMastery.getInstance().getDataFolder(),"goods.yml");
        FileConfiguration goodsConfig = PluginMastery.getInstance().getConfigManager().getConfig(fileGoodsConfig);
        if(!goodsConfig.contains("currency."+type)&&!type.equalsIgnoreCase("vault")){
            player.sendMessage(" §7§l[Mastery] "+PluginMastery.getInstance().getCurrent_lang().getString("unset1"));
            return;
        }
        ItemStack good = player.getItemInHand().clone();
        if(good.getType().equals(Material.AIR)){
            player.sendMessage(" §7§l[Mastery] "+PluginMastery.getInstance().getCurrent_lang().getString("unset2"));
            return;
        }
        Shop.setGoods(good,new Shop.Price(type,price));
        player.sendMessage(" §7§l[Mastery] "+PluginMastery.getInstance().getCurrent_lang().getString("set"));
    }

    @CommandHandler(
            playerOnly = true,
            name = "currency",
            permission = "mastery.admin",
            parameters = {"[类型名]","[等级]"})
    public void currency(){
        String type = args[0];
        int rank = Integer.valueOf(args[1]);
        File fileGoodsConfig = new File(PluginMastery.getInstance().getDataFolder(),"goods.yml");
        FileConfiguration goodsConfig = PluginMastery.getInstance().getConfigManager().getConfig(fileGoodsConfig);
        if(goodsConfig.contains("currency."+type)){
            player.sendMessage(" §7§l[Mastery] "+PluginMastery.getInstance().getCurrent_lang().getString("currency1"));
            return;
        }
        ItemStack currency = player.getItemInHand().clone();
        if(currency.getType().equals(Material.AIR)){
            player.sendMessage(" §7§l[Mastery] "+PluginMastery.getInstance().getCurrent_lang().getString("currency2"));
            return;
        }
        Shop.setCurrency(currency,type,rank);
        player.sendMessage(" §7§l[Mastery] "+PluginMastery.getInstance().getCurrent_lang().getString("currency"));
    }

    @CommandHandler(
            name = "currencylist",
            permission = "mastery.admin",
            parameters = {})
    public void currencyList(){
        int count = 1;
        sender.sendMessage("§e====================================");
        for(String name:Shop.getCurrencyList()){
            Shop.Currency currency = Shop.getCurrency(name);
            ItemStack item = currency.getItem();
            if(item==null) continue;
            int rank = currency.getRank();
            sender.sendMessage(count+"."+name+"§f  item: "+item.getType()+"§f rank: "+rank);
            count++;
        }
        sender.sendMessage("§e====================================");
    }

    @CommandHandler(
            name = "deletecurrency",
            permission = "mastery.admin",
            parameters = {"[货币类型]"})
    public void deleteCurrency(){
        String name = args[0];
        File fileGoodsConfig = new File(PluginMastery.getInstance().getDataFolder(),"goods.yml");
        FileConfiguration goodsConfig = PluginMastery.getInstance().getConfigManager().getConfig(fileGoodsConfig);
        if(!goodsConfig.contains("currency."+ name)){
            player.sendMessage(" §7§l[Mastery] "+PluginMastery.getInstance().getCurrent_lang().getString("deletecurrency1"));
            return;
        }
        Shop.deleteCurrency(name);
        player.sendMessage(" §7§l[Mastery] "+PluginMastery.getInstance().getCurrent_lang().getString("deletecurrency2"));
    }

    @CommandHandler(
            playerOnly = true,
            name = "deletegoods",
            permission = "mastery.admin",
            parameters = {})
    public void deleteGoods(){
        Shop.editGoodsList(player);
    }

    @CommandHandler(
            name = "currencygive",
            permission = "mastery.admin",
            parameters = {"[玩家名称]","[货币数量]","[货币类型]"})
    public void getCurrency(){
        File fileGoodsConfig = new File(PluginMastery.getInstance().getDataFolder(),"goods.yml");
        FileConfiguration goodsConfig = PluginMastery.getInstance().getConfigManager().getConfig(fileGoodsConfig);
        Player player = Bukkit.getPlayer(args[0]);
        if(player == null){
            sender.sendMessage(" §7§l[Mastery] "+PluginMastery.getInstance().getCurrent_lang().getString("currencygive2"));
            return;
        }
        int amount = Integer.valueOf(args[1]);
        if(amount<=0){
            sender.sendMessage(" §7§l[Mastery] "+PluginMastery.getInstance().getCurrent_lang().getString("currencygive3"));
            return;
        }
        String type = args[2];
        if(type.equalsIgnoreCase("vault")){
            Economy econ = PluginMastery.getInstance().getEcon();
            if(econ == null){
                sender.sendMessage(" §7§l[Mastery] "+PluginMastery.getInstance().getCurrent_lang().getString("vault"));
                return;
            }
            econ.depositPlayer(player,amount);
            String message=PluginMastery.getInstance().getCurrent_lang().getString("currencygive6")+"";
            message = message.replaceAll("%format%",econ.format(amount));
            player.sendMessage(" §7§l[Mastery] "+message);
            return;
        }
        ItemStack item = goodsConfig.getItemStack("currency."+type+".item");
        if(item==null||item.getType().equals(Material.AIR)){
            sender.sendMessage(" §7§l[Mastery] "+PluginMastery.getInstance().getCurrent_lang().getString("currencygive1"));
            return;
        }
        item.setAmount(amount);
        Map<Integer,ItemStack> surplus = player.getInventory().addItem(item);
        String message=PluginMastery.getInstance().getCurrent_lang().getString("currencygive4")+"";
        message = message.replaceAll("%amount%",amount+"").replaceAll("%type%",type);
        player.sendMessage(" §7§l[Mastery] "+message);
        for(Integer index:surplus.keySet()){
            ItemStack drop = surplus.get(index);
            List<ItemStack> listDrop = new ArrayList<>();
            while(drop.getAmount()>64){
                ItemStack copy = drop.clone();
                copy.setAmount(64);
                listDrop.add(copy);
                drop.setAmount(drop.getAmount()-64);
            }
            listDrop.add(drop);
            if(player.getLocation().getWorld() != null){
                for (ItemStack i: listDrop){
                    player.getLocation().getWorld().dropItem(player.getLocation(),i);
                }
            }
        }
        if(!surplus.isEmpty()) player.sendMessage(" §7§l[Mastery] "+PluginMastery.getInstance().getCurrent_lang().getString("currencygive5"));
    }

    @CommandHandler(
            name = "openinv",
            permission = "mastery.admin",
            parameters = {"[玩家名称]"})
    public void openInv(){
        Player player = Bukkit.getPlayer(args[0]);
        if(player == null) return;
        SellInventory sl = new SellInventory(player);
        sl.openView();
    }

    @Override
    protected void access() {

    }
}
